import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import Goodshearder from './goodshearder'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import './unpaid.scss'

export default class Index extends Component {
  constructor(props) {
  	super(props);
    this.state = {
      navindex:'UNPAID',//用于判断导航选中
      goodslist:[],
      pageheight:0,
	  	pageNumber:0,//页码数
      scrollPd:false,//是否可滑动
      hasNextPage:false,//是否有下一页
	  malltype:'',
	  appmark:CommonJs.getmark(),
    }
  }
  config = {
    navigationBarTitleText: '加载中'
  }

  componentWillMount () {
		var type=this.$router.preload.type;
		var malltype=this.$router.preload.malltype;
    var _this=this
    Taro.getSystemInfo({
      success:function(res){
        _this.setState({
          pageheight: res.windowHeight,
		  		navindex:type,
		  		malltype:malltype
        })
      }
    })
    setTimeout(function(){
		_this.getunpaid(0)
	},600)
  }
  componentDidMount () {}
  componentDidShow () {}
  getunpaid(pageNumber){
    var _this=this;
    if(this.state.navindex){
			var condition='&status.in='+this.state.navindex;
		}
    else{
    	var condition='&status.in=UNPAID'
    }
	if(this.state.navindex=='REFUND'){
		condition='&status.in=APPLY_REFUND&status.in=REFUND';
	}
	if(this.state.navindex=='CANCEL'){
		condition='&status.in=WITHDRAW&status.in=CANCEL';
	}
	if(this.state.malltype=='UC'){
		condition=condition+'&orderTypeId.equals=8'
		Taro.setNavigationBarTitle({title:"UC订单"})
	}
	else if(this.state.malltype=='WUC'){
		condition=condition+'&orderTypeId.equals=6'
		if(this.state.appmark=='xjzb'){
			Taro.setNavigationBarTitle({title:'WUC订单'})
		}
		else{
			Taro.setNavigationBarTitle({title:'积分订单'})
		}
	}
	else{
		if(this.state.appmark=='xjzb'){
			Taro.setNavigationBarTitle({title:'WGM订单'})
		}
		else{
			Taro.setNavigationBarTitle({title:'商品订单'})
		}
		condition=condition+'&orderTypeId.equals=1'
	}
    var url='/order/papi/v1.9/order?page='+pageNumber+condition+'&sort=createAt,DESC';
    CommonJs.ajaxRequest(url,'','GET',function(res){
		if(res.data.page<res.data.totalPages){
			var hasNextPage = true
		 }
		 else{
			var hasNextPage = false
		 }
		  
		  if(pageNumber==0){
  				  var results = []
  			}
  			else{
  				  var results = _this.state.goodslist
  			}
		  var result = res.data.list;
		  for(var i = 0; i < result.length; i++){
			results.push(result[i])
		  }
		  if (hasNextPage) {
			_this.setState({
			  goodslist:results,
			  pageNumber: res.data.page,
			  hasNextPage: hasNextPage,
			  scrollPd: true
			})
		  }
		  else {
			_this.setState({
			  goodslist:results,
			  pageNumber: res.data.page,
			  hasNextPage: hasNextPage,
			  scrollPd: false
			})
		  }

    },'wfw')
  }

  open(url){

    CommonJs.open(url);
  }
  change(type){
		this.setState({
		  navindex:type,
		  goodslist:[]
		})
		var _this=this
		setTimeout(function(){
			_this.getunpaid(0)
		},600)
  }
  loadMore(){
    var _this = this;
    var pd = _this.state.scrollPd;
    if(pd) {
      //防止多次触发滑动到底部事件
      _this.setState({
        scrollPd: false//禁止下拉
      })
      var pageNumber = _this.state.pageNumber + 1;
      _this.getunpaid(pageNumber)
    }else{
      var hasNextPage =  _this.state.hasNextPage;
      if(hasNextPage){
        Taro.showToast({
            title:'正在加载数据...',
            icon: 'none',
            duration: 2000
          });
      }else{
        Taro.showToast({
            title:'没有更多数据啦！',
            icon: 'none',
            duration: 2000
          });
      }
    }
  }
  ordertype(val){
	  if(val=='UNPAID'){
		  return '等待付款'
	  }
	  else if(val=='PAID'){
		  return '已付款'
	  }
	  else if(val=='GOODS_RECEIVED'){
		  return '等待收货'
	  }
	  else if(val=='COMPLETED'){
		  return '已完成'
	  }
	  else if(val=='WITHDRAW'){
		  return '等待取消审核'
	  }
	  else if(val=='CANCEL'){
		  return '已取消'
	  }
	  else if(val=='APPLY_REFUND'){
		  return '等待退款审核'
	  }
	  else if(val=='REFUND'){
		  return '已退款'
	  }
	  else{
		  return ''
	  }
  }
  paynow(id){
	 Taro.showToast({
		title:'暂无开通，敬请期待',
		icon: 'none',
		duration: 2000
	  });
  }
  qxtk(id){
	 var url='/order/papi/v1.9/order/cancel/applyRefund?orderId='+id
	 var _this=this;
	 Taro.showModal({
	   title: '操作提示',
	   content: '是否确定',
	   success(ret){
	 	  if(ret.confirm){
	 		CommonJs.ajaxRequest(url,'','PUT',function(res){
	 		  Taro.showToast({
	 			title:'操作成功，数据刷新中',
	 			icon: 'none',
	 			duration: 2000
	 		  });
	 		  setTimeout(function(){
	 			_this.getunpaid(0)
	 		  },2000)
	 		},'wfw')
	 	  }
	 	}
	 })
  }
  wlinfo(id){
  	 CommonJs.openTo("/pages/my/wlinfo?id="+id+"&type=1")
  }
  cancel(id){
	var url='/order/papi/v1.9/order/cancelOrder?orderId='+id
	var _this=this;
	Taro.showModal({
	  title: '操作提示',
	  content: '是否确定',
	  success(ret){
		  if(ret.confirm){
			CommonJs.ajaxRequest(url,'','PUT',function(res){
			  Taro.showToast({
				title:'操作成功，数据刷新中',
				icon: 'none',
				duration: 2000
			  });
			  setTimeout(function(){
				_this.getunpaid(0)
			  },2000)
			},'wfw')
		  }
		}
	})
  }
  tk(id){
	var url='/order/papi/v1.9/order/applyRefund?orderId='+id
	var _this=this;
	Taro.showModal({
	  title: '操作提示',
	  content: '是否确定',
	  success(ret){
		  if(ret.confirm){
			CommonJs.ajaxRequest(url,'','POST',function(res){
			  Taro.showToast({
				title:'操作成功，数据刷新中',
				icon: 'none',
				duration: 2000
			  });
			  setTimeout(function(){
				_this.getunpaid(0)
			  },2000)
			},'wfw')
		  }
		}
	})
  }
  sure(id){
	var url='/order/papi/v1.9/order/confirmReceipt?orderId='+id
	var _this=this;
	Taro.showModal({
	  title: '操作提示',
	  content: '是否确定',
	  success(ret){
		  if(ret.confirm){
			CommonJs.ajaxRequest(url,'','PUT',function(res){
			  Taro.showToast({
				title:'操作成功，数据刷新中',
				icon: 'none',
				duration: 2000
			  });
			  setTimeout(function(){
				_this.getunpaid(0)
			  },2000)
			},'wfw')
		  }
		}
	})
  }
  render () {
    const result = this.state.goodslist
    return (
      <View className='page'>
        <View className='qun-bgbanner'>
          <Goodshearder porpNav={this.state.navindex} mallNav={this.state.malltype} onHandOpen={this.change.bind(this)}></Goodshearder>
        </View>
        <ScrollView className='qun-goods' scrollY='true' style={'height:'+this.state.pageheight+'px'} onScrollToLower={this.loadMore.bind(this,'')}>
        <View className='qun-znwn' style={result.length!=0?'display:none':'display:block'}>暂无订单</View>
        {result.map((item, index) => (
        <View className='goodsorder-content'>
          <View className='goodsorder-unpaid'>
            <View className='unpaid-title'>
              <View className='qun-scallty'>
				<View className='unpaid-state'>{this.ordertype(item.status)}</View>
                <Image mode='scaleToFill' className='unpaid-photo' src='https://minichat.g2ma.com/kedouimg/store.png'></Image>
                <View className='unpaid-shopname'><Text className='unpaid-tel'>{item.shopName}</Text>({item.serialNo})</View>
              </View>
            </View>
            <View className='unpaid-content'>
              {item.orderItemVMS.map((item1, index1) => (
              <View className='unpaid-goods'>
				<View className='unpaid-pic'>
					<Image mode='scaleToFill' className='unpaid-pic-img' src={item1.itemPicture?item1.itemPicture:'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
                </View>
				<View className='unpaid-right'>
                  <View className='unpaid-goodsname'>{item1.itemName}</View>
                  <View className='unpaid-specifications'>规格：{item1.itemSku}</View>
                  <View className='unpaid-total'>
                    <View className='unpaid-num'>数量：{item1.amount}</View>
                    <View className='unpaid-price'>{item1.price/100}{this.state.malltype?this.state.malltype:'元'}</View>
                    
                  </View>
                </View>
              </View>
			  ))}
              <View className='yhq-list' style={item.orderCoupon?'display:block':'display:none'}>
                {item.orderCoupon!=null&&item.orderCoupon.map((item2, index2) => (
                <View className='yhq-li'><Text className='yhq-je'>-{item2.discountAmount/100}</Text>{item2.publishName}</View>
                ))}
              </View>
              <View className='unpaid-postage'>总：{item.totalAmount/100}{this.state.malltype?this.state.malltype:'元'},{item.postage != 0?'运费：'+item.postage/100:'包邮'},实付：{this.state.malltype?item.virtualCurrency/100:item.payAmount/100}{this.state.malltype?this.state.malltype:'元'}
              <View className='unpaid-time' style='float:left;text-aling:left;'>{CommonJs.FormatDate(item.createAt,3)}</View>
              </View>

            </View>
            <View className='unpaid-mes' style={item.inventoryType=='CLOUD'?'display:none':''}>
              <View className='unpaid-people'>收货人：{item.receiveName}  {item.receiveMobile}</View>
              <View className='unpaid-address'>收货地址：{item.regionName}{item.address}</View>
            </View>

            <View className='unpaid-order'>
              <Text className='unpaid-cancel' onClick={this.cancel.bind(this,item.id)} style={item.status=='UNPAID'?'display:':'display:none'}>取消订单</Text>
			  			<Text className='unpaid-cancel' onClick={this.paynow.bind(this,item.id)} style={item.status=='UNPAID'?'display:none':'display:none'}>继续支付</Text>
			  			<Text className='unpaid-cancel' onClick={this.sure.bind(this,item.id)} style={item.status=='GOODS_RECEIVED'?'display:':'display:none'}>确认收货</Text>
			  			<Text className='unpaid-cancel' onClick={this.tk.bind(this,item.id)} style={item.status!='UNPAID'&&item.status!='CANCEL'&&item.status!='REFUND' ?'display:':'display:none'}>申请退款</Text>
			  			<Text className='unpaid-cancel' onClick={this.qxtk.bind(this,item.id)} style={item.status=='APPLY_REFUND'?'display:':'display:none'}>取消退款申请</Text>
			  			<Text className='unpaid-cancel' onClick={this.wlinfo.bind(this,item.id)} style={item.status=='GOODS_RECEIVED'||item.status=='COMPLETED'?'display:':'display:none'}>查看物流信息</Text>
            </View>
          </View>
        </View>
        ))}
        </ScrollView>
      </View>
    )
  }
}
